import 'dart:async';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:kasax/kasax.dart';

class WebViewPage extends StatefulWidget {
  const WebViewPage({Key? key}) : super(key: key);

  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  var url = "";
  Completer<WebViewController> _controller = Completer<WebViewController>();

  // 加载状态
  bool isLoading = true;

  @override
  Widget build(BuildContext context) {
    var arguments = ModalRoute.of(context)!.settings.arguments;
    var title;
    if (arguments != null && arguments is Map) {
      var args = arguments as Map;
      if (args.containsKey('url')) {
        url = args['url'];
      }

      if (args.containsKey('title')) {
        title = args['title'];
      }
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Stack(
        children: [
          WebView(
            javascriptMode: JavascriptMode.unrestricted,
            onPageFinished: (String url) {
              print("完成加载: $url");
              setState(() {
                isLoading=false;
              });
            },
            onProgress: (pb) {
              print("加载: $url , pb: $pb");
              // setState(() {
              //   if (isLoading) isLoading = false;
              // });
            },
            onPageStarted: (String url) {
              print("开始加载: $url");
            },
            onWebViewCreated: (conroller) {
              _controller.complete(conroller);
            },
            initialUrl: url,
            allowsInlineMediaPlayback: true,
          ),
          isLoading ? buildBasicLoadingWidget() : Container()
        ],
      ),
    );
  }
}
